// pages/theme/theme.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeList: [],
    active: 0,
    tag: "",
    tagObj: {},
    themeDetailList: [],

  },
  async changeActive(e) {
    let active = e.currentTarget.dataset.index
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      active: active,
      tag: this.data.themeList[active].name
    })
    console.log(this.data.tag)
    this.getThemeDetail()
    await this.getThemeList()
    wx.hideLoading()
    wx.setStorageSync('poetName', this.data.poetName)
    // console.log(this.data.poetList[active])
  },
  getThemeKind() {
    let that = this;
    wx.request({
      url: 'http://localhost:3000/poetry/poetryThemeList2',
      method: "POST",
      data: {
        name: that.data.tag
      },
      success(data) {
        data.data.rows.unshift(that.data.tagObj)

        that.setData({
          themeList: data.data.rows
        })
        console.log(that.data.themeList, 555)

      }
    })
  },
  getThemeList() {
    let that = this;
    return new Promise((reslove, reject) => {
      wx.request({
        url: 'http://localhost:3000/poetry/poetryThemeDetail',
        method: "POST",
        data: {
          tag: that.data.tag
        },
        success(data) {
          console.log(data.data.rows)
          data.data.rows.map((item) => {
            item.poet = JSON.parse(item.poet)
            item.tags = JSON.parse(item.tags)
          })
          that.setData({
            themeDetailList: data.data.rows
          })
          reslove({
            code: 200,
            msg: "获取成功"
          })
        }
      })
    })

  },
  getThemeDetail() {
    let that = this
    wx.request({
      url: 'http://localhost:3000/poetry/poetryTheme',
      data: {
        name: this.data.tag
      },
      method: "POST",
      success(data) {
        console.log(data.data.rows[0])
        that.setData({
          tagObj: data.data.rows[0]
        })
      }
    })
  },
  jumpGO(e) {
    console.log(e.currentTarget.dataset.msg);
    wx.setStorageSync('poetName', e.currentTarget.dataset.name)

    wx.setStorageSync('msg', e.currentTarget.dataset.msg)
    wx.navigateTo({
      url: '../detail/detail',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    wx.showLoading({
      title: '加载中',
    })
    this.setData({
      tag: wx.getStorageSync('theme')
    })
    this.getThemeDetail()
    this.getThemeKind()
    await this.getThemeList()
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})